package com.ms.game.sendgift.db;

import java.util.ArrayList;

import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.sendgift.message.SendGiftNotifyMessage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class SaveGiftNotifyAnswerAsync extends TaskAsyncCommand {
	
	/**
	 *  MessageTypes.S_GIFT_SECOND_ACCEPT_RESPONSE  
	 *  MessageTypes.S_GIFT_FINIAL_GIVE_RESPONSE
	 */
	public int messageType;
	
	/**
	 * 帳單ID
	 */
	public long orderID;
	
	/**
	 * 訂單資料
	 */
	public SendGiftNotifyModel model;
	
	public GUID sessionID;
	
	public String playerName;
	
	public String playerIP;
	
	public GUID targetGUID;
	
	public String targetIP;
	
	public boolean success = false;
	
	public SendGiftResponse response;
		
	private SendGiftRecordLog logModel;
	

	private boolean isSaveDone = false;

	@Override
	public void asyncExecute() throws Exception {
			
			logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setPlayerIP(playerIP);
			logModel.setTargetIP(targetIP);
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			logModel.setCompleteTime(model.getFinishTime());
			logModel.setOverTime(model.getOverTime());
			logModel.setOrderFinishTime(model.getOrderFinishTime());
			logModel.setSuccess(success);
			logModel.setFinish(model.isFinish());
			logModel.setTaken(model.isTaken());	
			
			SendGiftRecordDAO.saveOrUpdateGiftNotify(logModel);
	}

	/** 
	 * DC callback
	 * @see GameLobbyModule#sendGiftCallBack(BaseDataInfo, Object...)
	 */
	@Override
	public void execute() throws Exception {
		
		long createTime = model.getCreateTime().getTime();

		// 跟自己說成功
		response.createTime = createTime;
		response.errorCode = ErrorCode.SUCCESS;
		response.orderID = model.getSendOrderID();
		MessageAction.sendMessage(sessionID, messageType, response);

		// 回覆對方
		int returnMessage = 0;
		if(messageType == MessageTypes.S_GIFT_SECOND_ACCEPT_RESPONSE){
			returnMessage = MessageTypes.S_GIFT_SECOND_RETURN_RESPONSE;
		}else if(messageType == MessageTypes.S_GIFT_FINIAL_GIVE_RESPONSE){
			returnMessage =  MessageTypes.S_GIFT_FINIAL_RETURN_RESPONSE;
		}

		GUID targetSession = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(targetGUID);
		if (targetSession != null) {
			// 如果對方在線上
			SendGiftNotifyMessage notify = new SendGiftNotifyMessage();
			notify.answer = response.answer;
			notify.fromName = playerName;
			notify.orderID = model.getSendOrderID();
			notify.orderNumber = model.getOrderNumber();
			notify.createTime = createTime;
			MessageAction.sendMessage(targetSession, returnMessage, notify);
		}else{
			ArrayList<String> players = new ArrayList<String>();
			players.add(targetGUID.toString());
		}
		
	}

	public boolean isSaveDone() {
		return isSaveDone;
	}
	
	public SendGiftRecordLog getLogModel() {
		return logModel;
	}
}
